home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Jotto2.so Folder / Jotto ][ ƒ / MSG Shell ƒ / msg graphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-10  |  12.4 KB  |  531 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg graphics.c
  4.  
  5. Purpose:    This module handles the about box, about MSG box, main
  6.             window opening/closing/updating, and help windows
  7.             opening/closing/updating.
  8.  
  9.  
  10. Jotto ][ -=- a simple word game, revisited
  11. Copyright (C) 1993 Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "msg graphics.h"
  31. #include "msg dialogs.h"
  32. #include "msg error.h"
  33. #include "msg menus.h"
  34. #include "msg sounds.h"
  35. #include "msg environment.h"
  36. #include "msg prefs.h"
  37. #include "jotto globals.h"
  38. #include "jotto graphics.h"
  39. #include "jotto help.h"
  40. #include "wipe dispatch.h"
  41. #include "alternate about.h"
  42. #include "text scroll.h"
  43. #include "util.h"
  44.  
  45. Boolean            gInitedWindowBounds;
  46. Rect            gMainScreenBounds;
  47. Rect            gMainWindowBounds;
  48. GDHandle        gBiggestDevice;
  49. WindowPtr        gMainWindow;
  50. WindowPtr        gHelp[NUM_HELP];
  51. short                gHelpWidth;
  52. short                gHelpHeight;
  53. short                gWindowWidth;
  54. short                gWindowHeight;
  55.  
  56. /* internal stuff */
  57. Rect            bRect;
  58. Ptr                myBits;
  59. CGrafPort        myCGrafPort;
  60. CGrafPtr        myCGrafPtr;
  61. CTabHandle        ourCMHandle;
  62. GrafPort        myGrafPort;
  63. GrafPtr            myGrafPtr;
  64.  
  65. void InitMSGGraphics(void)
  66. {
  67.     gWindowWidth=gWindowHeight=gHelpWidth=300;
  68.     gHelpHeight=200;
  69.     myCGrafPtr=0L;
  70.     myGrafPtr=0L;
  71. }
  72.  
  73. void ShowInformation(void)
  74. {
  75.     DialogPtr        theDlog;
  76.     short            itemSelected = 0;
  77.     short                newleft;
  78.     short                newtop;
  79.     short                dlogID;
  80.     KeyMap            keys;
  81.     
  82.     GetKeys(keys);
  83.     if ((keys[2]&0x0400) && (keys[2]&0x8000))
  84.         AlternateInformation();
  85.     else
  86.     {
  87.         if(GetWindowDepth() > 2)
  88.             dlogID = colorInfoDialog;
  89.         else
  90.             dlogID = bwInfoDialog;
  91.         
  92.         theDlog = GetNewDialog(dlogID, 0L, (WindowPtr)-1L);
  93.         if (keys[3]&4)
  94.             SizeWindow(theDlog, 300, 200, true);
  95.         newleft = gMainScreenBounds.left + (((gMainScreenBounds.right -
  96.                     gMainScreenBounds.left) - (theDlog->portRect.right -
  97.                     theDlog->portRect.left)) / 2);
  98.         newtop = gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  99.                     gMainScreenBounds.top) - (theDlog->portRect.bottom -
  100.                     theDlog->portRect.top)) / 2);
  101.         if(newtop < 15)
  102.             newtop = 15;
  103.         MoveWindow(theDlog, newleft, newtop, TRUE);
  104.         ShowWindow(theDlog);
  105.         
  106.         while(itemSelected == 0)
  107.         {
  108.             ModalDialog(nil, &itemSelected);
  109.         }
  110.         HideWindow(theDlog);
  111.         DisposeDialog(theDlog);
  112.     }
  113. }
  114.  
  115. void ShowSplashScreen(void)
  116. {
  117.     short                dlogID;
  118.     DialogPtr        theDlog;
  119.     short            itemSelected = 0;
  120.     short                newleft;
  121.     short                newtop;
  122.     KeyMap            keys;
  123.     short            itemType;
  124.     Handle            itemH;
  125.     Rect            box;
  126.     unsigned char    temp[7];
  127.     ModalFilterUPP gProcOFilterRD = NewModalFilterProc(ProcOFilter);
  128.     
  129.     GetKeys(keys);
  130.     
  131.     if(GetWindowDepth() > 2)
  132.         dlogID = colorSplashDialog;
  133.     else
  134.         dlogID = bwSplashDialog;
  135.     
  136.     theDlog = GetNewDialog(dlogID, 0L, (WindowPtr)-1L);
  137.     newleft = gMainScreenBounds.left + (((gMainScreenBounds.right -
  138.                 gMainScreenBounds.left) - (theDlog->portRect.right -
  139.                 theDlog->portRect.left)) / 2);
  140.     newtop = gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  141.                 gMainScreenBounds.top) - (theDlog->portRect.bottom -
  142.                 theDlog->portRect.top)) / 2);
  143.     if(newtop < 15)
  144.         newtop = 15;
  145.     MoveWindow(theDlog, newleft, newtop, TRUE);
  146.     
  147.     StuffHex(temp, (gNumLetters==5) ? "\p054348454154" : "\p06534E45414B59");
  148.     if (Mymemcompare((Ptr)gHumanWord[gNumTries], (Ptr)((long)temp+1), gNumLetters))
  149.     {
  150.         Mymemcpy((Ptr)((long)temp+1), gComputerWord, gNumLetters);
  151.         ParamText(temp, "\p", "\p", "\p");
  152.     }
  153.     else if (keys[2]&0x0400)
  154.     {
  155.         GetDItem(theDlog, 3, &itemType, (Handle*)&itemH, &box);
  156.         SetIText((Handle)itemH, "\p");
  157.         ParamText("\pForgive your enemies, but", "\pnever forget their names. -JFK","\p","\p");
  158.     }
  159.     else
  160.         ParamText(gMyName,gMyOrg,"\p","\p");
  161.  
  162.     DoSound(sound_aboutMSG);
  163.     OpenTheSoundDevice();
  164.     ShowWindow(theDlog);
  165.     while(itemSelected == 0)
  166.         ModalDialog(gProcOFilterRD, &itemSelected);
  167.     
  168.     HideWindow(theDlog);
  169.     DisposeDialog(theDlog);
  170.     CloseTheSoundDevice();
  171.     DisposeRoutineDescriptor(gProcOFilterRD);
  172. }
  173.  
  174. void OpenMainWindow(void)
  175. {
  176.     if(!gMainWindow)
  177.     {
  178.         if(!gInitedWindowBounds)
  179.         {
  180.             gMainWindowBounds.left = gMainScreenBounds.left + (((gMainScreenBounds.right -
  181.                         gMainScreenBounds.left) - gWindowWidth) / 2);
  182.             gMainWindowBounds.top = 9+ gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  183.                         gMainScreenBounds.top) - gWindowHeight) / 2);
  184.             if(gMainWindowBounds.top < 30)
  185.                 gMainWindowBounds.top = 30;
  186.             gMainWindowBounds.bottom = gMainWindowBounds.top + gWindowHeight;
  187.             gMainWindowBounds.right = gMainWindowBounds.left + gWindowWidth;
  188.             gInitedWindowBounds = 1;
  189.         }
  190.         
  191.         if(gHasColorQD)
  192.         {
  193.             gMainWindow = NewCWindow(0L, &gMainWindowBounds, "\pJotto ][", TRUE,
  194.                     noGrowDocProc, (WindowPtr)-1L, TRUE, 0L);
  195.         }
  196.         else
  197.         {
  198.             gMainWindow = NewWindow(0L, &gMainWindowBounds, "\pJotto ][", TRUE,
  199.                     noGrowDocProc, (WindowPtr)-1L, TRUE, 0L);
  200.         }
  201.  
  202.         bRect = gMainWindow->portRect;
  203.     }
  204.     
  205.     if(gMainWindow)
  206.     {
  207.         SetPort(gMainWindow);
  208.         InvalRect(&(gMainWindow->portRect));
  209.     }
  210.     else ErrorString("\pThere is not enough memory to open the main window.","\p");
  211. }
  212.  
  213. void GetMainScreenBounds(void)
  214. {
  215.     gMainScreenBounds = qd.screenBits.bounds;
  216.     gMainScreenBounds.top += MBarHeight;
  217. }
  218.  
  219. short GetWindowDepth(void)
  220. {
  221.     Rect        tempRect;
  222.     long        biggestSize;
  223.     long        tempSize;
  224.     GDHandle    thisHandle;
  225.     
  226.     if(gHasColorQD)
  227.     {
  228.         if(gMainWindow)
  229.         {
  230.             thisHandle = GetDeviceList();
  231.             gBiggestDevice = 0L;
  232.             biggestSize = 0L;
  233.             
  234.             while(thisHandle)
  235.             {
  236.                 if(TestDeviceAttribute(thisHandle, screenDevice) &&
  237.                             TestDeviceAttribute(thisHandle, screenActive))
  238.                     if(SectRect(&(gMainWindow->portRect), &((**thisHandle).gdRect),
  239.                                 &tempRect))
  240.                         if(biggestSize < (tempSize =
  241.                                 ((long)(tempRect.bottom - tempRect.top))
  242.                                 * ((long)(tempRect.right - tempRect.left))))
  243.                         {
  244.                             biggestSize = tempSize;
  245.                             gBiggestDevice = thisHandle;
  246.                         }
  247.                 thisHandle = GetNextDevice(thisHandle);
  248.             }
  249.             
  250.             if(gBiggestDevice)
  251.                 return (**(**gBiggestDevice).gdPMap).pixelSize;
  252.             else
  253.                 return 1;
  254.         }
  255.         else
  256.         {
  257.             return (**(**GetMainDevice()).gdPMap).pixelSize;
  258.         }
  259.     }
  260.     else
  261.     {
  262.         return 1;
  263.     }
  264. }
  265.  
  266. void UpdateBoard(void)
  267. {
  268.     long        offRowBytes, sizeOfOff;
  269.     short            theDepth, i, err;
  270.     GDHandle    oldDevice;
  271.     static short    gLastDepth;
  272.  
  273.     if((theDepth = GetWindowDepth()) > 2)
  274.     {
  275.         /* if we just changed from one color depth to another color depth */
  276.         if((myCGrafPtr != 0) && (gLastDepth != theDepth))
  277.         {
  278.             DisposeHandle((Handle)(**(myCGrafPort).portPixMap).pmTable);
  279.             DisposePtr((**(myCGrafPort).portPixMap).baseAddr);
  280.             CloseCPort(myCGrafPtr);
  281.             myCGrafPtr = 0;
  282.         }
  283.         
  284.         if (myCGrafPtr==0L)
  285.         {
  286.             /* if we just switched from b/w to color, delete the b/w port */
  287.             if(myGrafPtr != 0L)
  288.             {
  289.                 DisposePtr(myGrafPort.portBits.baseAddr);
  290.                 ClosePort(myGrafPtr);
  291.                 myGrafPtr = 0;
  292.             }
  293.             
  294.             if(gBiggestDevice)
  295.             {
  296.                 oldDevice = GetGDevice();
  297.                 SetGDevice(gBiggestDevice);
  298.             }
  299.             else
  300.                 oldDevice = 0;
  301.             
  302.             myCGrafPtr = &myCGrafPort;
  303.             OpenCPort(myCGrafPtr);
  304.             gLastDepth = theDepth = (**(myCGrafPort).portPixMap).pixelSize;
  305.             
  306.             offRowBytes = (((theDepth * (bRect.right - bRect.left)) + 15) >> 4) << 1;
  307.             sizeOfOff = (long)(bRect.bottom - bRect.top) * offRowBytes;
  308.             OffsetRect(&bRect, -bRect.left, -bRect.top);
  309.             
  310.             myBits = NewPtr(sizeOfOff);
  311.             if(myBits == 0L)
  312.                 ErrorString("\pThere is not enough memory to open the main window.","\p");
  313.             
  314.             (**(myCGrafPort).portPixMap).baseAddr = myBits;
  315.             (**(myCGrafPort).portPixMap).rowBytes = offRowBytes + 0x8000;
  316.             (**(myCGrafPort).portPixMap).bounds = bRect;
  317.             
  318.             myCGrafPort.portRect = bRect;
  319.             
  320.             ourCMHandle = (**(**gBiggestDevice).gdPMap).pmTable;
  321.             err = HandToHand((Handle*)&ourCMHandle);
  322.             if(err != noErr)
  323.                 ErrorString("\pThere is not enough memory to open the main window.","\p");
  324.             
  325.             for(i = 0; i <= (**ourCMHandle).ctSize; i++)
  326.                 (**ourCMHandle).ctTable[i].value = i;
  327.             (**ourCMHandle).ctFlags &= 0x7fff;
  328.             (**ourCMHandle).ctSeed = GetCTSeed();
  329.             
  330.             (**(myCGrafPort).portPixMap).pmTable = ourCMHandle;
  331.             
  332.             if(oldDevice)
  333.                 SetGDevice(oldDevice);
  334.         }
  335.         
  336.         UpdateBoardColor();
  337.     }
  338.     else
  339.     {
  340.         if (myGrafPtr==0L)
  341.         {
  342.             if(myCGrafPtr != 0)
  343.             {
  344.                 DisposeHandle((Handle)(**(myCGrafPort).portPixMap).pmTable);
  345.                 DisposePtr((**(myCGrafPort).portPixMap).baseAddr);
  346.                 CloseCPort(myCGrafPtr);
  347.                 myCGrafPtr = 0;
  348.             }
  349.             
  350.             myGrafPtr = &myGrafPort;
  351.             OpenPort(myGrafPtr);
  352.             
  353.             offRowBytes = (((bRect.right - bRect.left) + 15) >> 4) << 1;
  354.             sizeOfOff = (long)(bRect.bottom - bRect.top) * offRowBytes;
  355.             OffsetRect(&bRect, -bRect.left, -bRect.top);
  356.             
  357.             myBits = NewPtr(sizeOfOff);
  358.             if(myBits == 0L)
  359.                 ErrorString("\pThere is not enough memory to open the main window.", "\p");
  360.             
  361.             myGrafPort.portBits.baseAddr = myBits;
  362.             myGrafPort.portBits.rowBytes = offRowBytes;
  363.             myGrafPort.portBits.bounds = bRect;
  364.             myGrafPort.portRect = bRect;
  365.         }
  366.         
  367.         UpdateBoardBW();
  368.     }
  369. }
  370.  
  371. void UpdateBoardColor(void)
  372. {
  373.     GDHandle    oldDevice;
  374.     RgnHandle    oldClipRgn;
  375.     
  376.     SetPort(gMainWindow);
  377.     
  378.     oldDevice = GetGDevice();
  379.     SetGDevice(gBiggestDevice);
  380.     
  381.     oldClipRgn = myCGrafPort.clipRgn;
  382.     myCGrafPort.clipRgn = gMainWindow->visRgn;
  383.     
  384.     SetPort((GrafPtr)myCGrafPtr);
  385.     
  386.     DrawBoardColor();
  387.     
  388.     SetPort(gMainWindow);
  389.     SetGDevice(oldDevice);
  390.     
  391.     if (gIsEndGame)
  392.         DoTheDissolve((GrafPtr)myCGrafPtr);
  393.     else if ((gWhichWipe!=0) && (gAnimation))
  394.         DoTheWipe((GrafPtr)myCGrafPtr);
  395.     else
  396.         CopyBits(&(((GrafPtr)myCGrafPtr)->portBits),
  397.                     &(gMainWindow->portBits), &bRect, &bRect, 0, 0L);
  398.     
  399.     myCGrafPort.clipRgn = oldClipRgn;
  400. }
  401.  
  402. void UpdateBoardBW(void)
  403. {
  404.     RgnHandle    oldClipRgn;
  405.     
  406.     SetPort(gMainWindow);
  407.     
  408.     oldClipRgn = myGrafPort.clipRgn;
  409.     myGrafPort.clipRgn = gMainWindow->visRgn;
  410.     
  411.     SetPort(myGrafPtr);
  412.     
  413.     DrawBoardBW();
  414.     
  415.     SetPort(gMainWindow);
  416.     
  417.     if (gIsEndGame)
  418.         DoTheDissolve(myGrafPtr);
  419.     else if ((gWhichWipe!=0) && (gAnimation))
  420.         DoTheWipe(myGrafPtr);
  421.     else
  422.         CopyBits(&(myGrafPtr->portBits),
  423.                     &(gMainWindow->portBits), &bRect, &bRect, 0, 0L);
  424.     
  425.     myGrafPort.clipRgn = oldClipRgn;
  426. }
  427.  
  428. void CloseMainWindow(void)
  429. {
  430.     DisposeWindow(gMainWindow);
  431.     gMainWindow=0L;
  432.     
  433.     gInProgress=FALSE;
  434.     AdjustMenus();
  435. }
  436.  
  437. void ShutDownMSGGraphics(void)
  438. {
  439.     if(myCGrafPtr != 0)
  440.     {
  441.         DisposeHandle((Handle)(**(myCGrafPort).portPixMap).pmTable);
  442.         DisposePtr((**(myCGrafPort).portPixMap).baseAddr);
  443.         CloseCPort(myCGrafPtr);
  444.         myCGrafPtr = 0;
  445.     }
  446.     if(myGrafPtr != 0L)
  447.     {
  448.         DisposePtr(myGrafPort.portBits.baseAddr);
  449.         ClosePort(myGrafPtr);
  450.         myGrafPtr = 0;
  451.     }
  452.     if ((myCGrafPtr!=0L) || (myGrafPtr!=0L))
  453.         DisposPtr(myBits);
  454. }
  455.  
  456. void OpenHelpWindow(short whichHelp)
  457. {
  458.     Rect    helpRect;
  459.     
  460.     if (!(gHelp[whichHelp]))
  461.     {
  462.         helpRect.left=10+20*whichHelp;
  463.         helpRect.top=50+20*whichHelp;
  464.         helpRect.bottom=helpRect.top+gHelpHeight;
  465.         helpRect.right=helpRect.left+gHelpWidth;
  466.         
  467.         gHelp[whichHelp]=NewWindow(0L, &helpRect, "\p", TRUE, noGrowDocProc, 
  468.                                     (WindowPtr)-1L, TRUE, 0L);
  469.         SetHelpTitle(whichHelp);
  470.     }
  471.     
  472.     SetPort(gHelp[whichHelp]);
  473.     InvalRect(&((gHelp[whichHelp])->portRect));
  474. }
  475.  
  476. void SetHelpTitle(short whichHelp)
  477. {
  478.     Str255        name;
  479.     
  480.     GetItem(gHelpMenu, whichHelp + 1, name);
  481.     SetWTitle(gHelp[whichHelp], name);
  482. }
  483.  
  484. void UpdateHelp(short whichHelp)
  485. {
  486.     long        offRowBytes, sizeOfOff;
  487.     Ptr            myBits;
  488.     Rect        bRect;
  489.     GrafPort    myGrafPort;
  490.     GrafPtr        myGrafPtr;
  491.     RgnHandle    oldClipRgn;
  492.     
  493.     SetPort(gHelp[whichHelp]);
  494.     
  495.     bRect = (gHelp[whichHelp])->portRect;
  496.     
  497.     myGrafPtr = &myGrafPort;
  498.     OpenPort(myGrafPtr);
  499.     
  500.     offRowBytes = ((gHelpWidth + 15) >> 4) << 1;
  501.     sizeOfOff = (long)(gHelpHeight) * offRowBytes;
  502.     OffsetRect(&bRect, -bRect.left, -bRect.top);
  503.     
  504.     myBits = NewPtr(sizeOfOff);
  505.     if(myBits == 0L)
  506.         ErrorString("\pThere is not enough memory.  ", "\p");
  507.     
  508.     myGrafPort.portBits.baseAddr = myBits;
  509.     myGrafPort.portBits.rowBytes = offRowBytes;
  510.     myGrafPort.portBits.bounds = bRect;
  511.     
  512.     myGrafPort.portRect = bRect;
  513.     
  514.     oldClipRgn = myGrafPort.clipRgn;
  515.     myGrafPort.clipRgn = (gHelp[whichHelp])->visRgn;
  516.     
  517.     SetPort(myGrafPtr);
  518.     
  519.     DrawHelp(whichHelp);
  520.     
  521.     SetPort(gHelp[whichHelp]);
  522.     
  523.     CopyBits(&(myGrafPtr->portBits),
  524.         &((gHelp[whichHelp])->portBits), &bRect, &bRect, 0, 0L);
  525.     
  526.     myGrafPort.clipRgn = oldClipRgn;
  527.     
  528.     ClosePort(myGrafPtr);
  529.     DisposPtr(myBits);
  530. }
  531.